<%@ page import="emp.entity.User" %>
<%@ page import="emp.entity.Employee" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加员工</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 0;
    }
    .container {
      width: 100%;
      max-width: 500px;
      margin: 50px auto;
      padding: 20px;
      background-color: #fff;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
    }
    h2 {
      text-align: center;
    }
    label {
      display: block;
      margin: 10px 0 5px;
    }
    input[type="text"], input[type="password"], select {
      width: 100%;
      padding: 8px;
      margin: 5px 0 15px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    button {
      width: 100%;
      padding: 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      cursor: pointer;
    }
    button:hover {
      background-color: #45a049;
    }
    .message {
      text-align: center;
      font-size: 14px;
      margin-top: 20px;
    }
  </style>
</head>
<body>
<div class="container">
  <%
    // 假设id是从request中获取的，如果有的话
    String id = request.getParameter("id");
    // 获取从 Servlet 传递来的用户信息
    Employee e = (Employee) request.getAttribute("employee");
  %>
  <h2>添加用户</h2>
  <!-- 表单提交到 /addUser.jsp -->
  <form action="employee?<%= id == null ? "action=add" : "action=update&id=" + id %>" method="POST">
    <!-- 员工姓名 -->
    <label for="empName">员工姓名</label>
    <input type="text" id="empName" name="empName"
           value="<%= (e != null) ? e.getEmpName() : "" %>" required>

    <!-- 性别 -->
    <label for="gender">性别</label>
    <select id="gender" name="gender" required>
      <option value="1" <%= (e != null && e.getGender() == '1') ? "selected" : "" %>>男</option>
      <option value="2" <%= (e != null && e.getGender() == '2') ? "selected" : "" %>>女</option>
    </select>

    <!-- 年龄 -->
    <label for="age">年龄</label>
    <input type="number" id="age" name="age"
           value="<%= (e != null) ? e.getAge() : "" %>" required>

    <!-- 工资 -->
    <label for="salary">工资</label>
    <input type="number" id="salary" name="salary"
           value="<%= (e != null) ? e.getSalary() : "" %>" required>
    <!-- 工资 -->
    <label for="hometown">hometown</label>
    <input type="text" id="hometown" name="hometown"
           value="<%= (e != null) ? e.getHometown() : "" %>" required>
    <!-- 工资 -->
    <label for="phone">电话</label>
    <input type="text" id="phone" name="phone"
           value="<%= (e != null) ? e.getPhone() : "" %>" required>

    <!-- 地址 -->
    <label for="address">地址</label>
    <input type="text" id="address" name="address"
           value="<%= (e != null) ? e.getAddress() : "" %>" required>
    <label for="edu">学历</label>
    <select id="edu" name="edu">
      <!-- 遍历 optionsList 并生成 <option> 标签 -->
      <c:forEach var="option" items="${eduList}">
        <option value="${option.eduId}">${option.eduLevel}</option>
      </c:forEach>
    </select>
    <label for="dept">部门</label>
    <select id="dept" name="dept">
      <!-- 遍历 optionsList 并生成 <option> 标签 -->
      <c:forEach var="option" items="${deptList}">
        <option value="${option.deptId}">${option.deptName}</option>
      </c:forEach>
    </select>


    <button type="submit">
      <%= (id == null || id.isEmpty()) ? "添加用户" : "修改" %>
    </button>

  </form>

  <div class="message">
    <c:if test="${not empty successMessage}">
      <span style="color:green">${successMessage}</span>
    </c:if>
    <c:if test="${not empty errorMessage}">
      <span style="color:red">${errorMessage}</span>
    </c:if>
  </div>
</div>
</body>
</html>


